package vip.liux.front.domain.models.chat;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;
import vip.liux.front.domain.enums.ChatMessageSide;

import java.time.Instant;

/**
 * 聊天用户消息
 */
@Getter
@Setter
@Entity
@Table(name = "chat_user_messages")
@org.springframework.data.relational.core.mapping.Table("chat_user_messages")
public class ChatUserMessage extends vip.liux.domain.entities.Entity<Long> {

    /**
     * 租户ID
     */
    @Column(name = "tenant_id")
    private String tenantId;

    /**
     * 聊天消息ID
     */
    @NotNull
    @Column(name = "chat_message_id", nullable = false)
    private Long chatMessageId;

    /**
     * 用户ID
     */
    @NotNull
    @Column(name = "user_id", nullable = false)
    private Long userId;

    /**
     * 目标用户ID
     */
    @Column(name = "target_user_id")
    private Long targetUserId;

    /**
     * 发送方/接收方
     * @see ChatMessageSide
     */
    @Column(name = "side")
    private Integer side;

    /**
     * 是否已读
     */
    @NotNull
    @Column(name = "read", nullable = false)
    private boolean read;

    /**
     * 已读时间
     */
    @Column(name = "read_time")
    private Instant readTime;

    public ChatUserMessage(Long userId, Long chatMessageId, ChatMessageSide side, Long targetUserId, String tenantId) {
        this.userId = userId;
        this.chatMessageId = chatMessageId;
        this.side = side.ordinal();
        this.targetUserId = targetUserId;
        this.tenantId = tenantId;
    }

    public ChatUserMessage() {

    }

    /**
     * 标记为已读
     *
     * @param now 当前时间
     */
    public void markAsRead(Instant now) {
        this.read = true;
        this.readTime = now;
    }
}